C++ Patterns: Executing Around Sequences

نویسنده

  • Kevlin Henney
چکیده

Sequential control flow forms the backbone of any procedural program. Just as common function, data, and structure in a C++ program can be factored out using functions, classes, and templates, common control flow schemas can also be named and abstracted. One such recurring schema, or programming cliché, is the embracing or bracketing of a sequence by a pair of actions, such as resource acquisition and release operations executed around the actual resource usage code. The patterns in this paper document idiomatic practices for abstracting such control flow in C++. They are connected into a language and further explored through a narrative example. Preand Post-Sequence Actions Paired actions – where a function is called before some statement sequence and a corresponding function afterwards – are commonly associated with resource acquisition and release. Preand post-sequence actions are a common feature of block-scoped resource management – e.g. allocate memory, use it, deallocate it – and this programming cliché can be found time and time again across many programs. Such repetition suggests a missing abstraction, and is both error prone (from the programming perspective) and prone in the presence of error (not exception safe). What is needed is a way to capture this control flow form, encapsulating it so that it is simple and exception safe. Exceptions, Complexity, and Abstraction Idioms are context dependent. As with multi-threading, the presence of exceptions invalidates many assumptions and practices, blowing working code out of the water and into the deep abyss of debugging. Exceptions make a handful of long-standing and well-respected C++ idioms inappropriate for modern C++ [ISO1998]. For instance, the basic rules for safe assignment have changed from the ORTHODOX CANONICAL CLASS FORM [Coplien1992] to post-standard C++ [Henney1998, Sutter2000], such that the original form is no longer considered safe. Adopting old styles with new rules tends to lead to disgraceful rather than graceful failure, and exceptions are an inevitable feature of modern C++ programs. Perhaps the greatest upset to any well-laid plan of action is something out of the ordinary: The least favorite chore for most programmers is handling exceptions! The number of ways something can go wrong seems to outnumber the ways it can go right, leading to exception code that dwarfs the core program logic. Standard C++ provides a general exception-handling model that addresses the control and information flow issues. However, as it is non-local in effect it also affects the way the conventional control flow is – or should be – expressed. In many modern programs exceptions are not something that can be ignored safely, although there are cases when they can be avoided. For instance, errors that are expected rather than exceptional should rely on more traditional schemes of communication, such as returning error codes, e.g. failure to open a file. The presence of exceptions in a program can have a significant impact. It is possible, if care is not taken, for an object to be left in an indeterminate state, thereby leaving the whole program in an unstable state. Safety – and hence stability – can be classed at one of three levels [Sutter2000]: The basic guarantee states that in the presence of exceptions, the throwing object will not leak resources. The object will be in a stable and usable, albeit not necessarily predictable, state. The strong guarantee states that the program's state will remain unchanged in the presence of exceptions, that is, commit-or-rollback semantics. The nothrow guarantee promises that exceptions are never thrown. In code, exception safety can be achieved in one of two ways: Code may be scaffolded explicitly with try, catch, and throw to ensure explicit and graceful recovery. Exception-neutral code is code that works in the presence of exceptions without requiring explicit exception-handling code. Such code can also be described as exception agnostic. A sign of weakness in a class design is that it forces its user to remember lots of particular and subtle conditions of use: "function a must be called before b, unless condition c is true, in which case d must follow e, etc.". Such interfaces suggest that the design is incomplete, the user of the class is doing work that a good design would not require: Their code is verbose and repetitive, and writing it is error prone.

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

High Fuzzy Utility Based Frequent Patterns Mining Approach for Mobile Web Services Sequences

Nowadays high fuzzy utility based pattern mining is an emerging topic in data mining. It refers to discover all patterns having a high utility meeting a user-specified minimum high utility threshold. It comprises extracting patterns which are highly accessed in mobile web service sequences. Different from the traditional fuzzy approach, high fuzzy utility mining considers not only counts of mob...

متن کامل

Sequence Mining in Dynamic and Interactive Environments

The discovery of frequent sequences in temporal databases is an important data mining problem. Most current work assumes that the database is static, and a database update requires rediscovering all the patterns by scanning the entire old and new database. In this paper, we propose novel techniques for maintaining sequences in the presence of a) database updates, and b) user interaction (e.g. m...

متن کامل

Distinct striatal regions for planning and executing novel and automated movement sequences

The basal ganglia-thalamo-cortical circuits are viewed as segregated parallel feed back loops crucially involved in motor control, cognition, and emotional processing. Their role in planning novel, as compared to overlearned movement patterns is as yet not well defined. We tested for the involvement of the associative striatum (caudate/anterior putamen) in the generation of novel movement patte...

متن کامل

Diversity of preferred nucleotide sequences around the translation initiation codon in eukaryote genomes

Understanding regulatory mechanisms of protein synthesis in eukaryotes is essential for the accurate annotation of genome sequences. Kozak reported that the nucleotide sequence GCCGCC(A/G)CCAUGG (AUG is the initiation codon) was frequently observed in vertebrate genes and that this 'consensus' sequence enhanced translation initiation. However, later studies using invertebrate, fungal and plant ...

متن کامل

Parallel processing of serial movements in prefrontal cortex.

A key idea in Lashley's formulation of the problem of serial order in behavior is the postulated neural representation of all serial elements before the action begins. We studied this question by recording the activity of individual neurons simultaneously in small ensembles in prefrontal cortex while monkeys copied geometrical shapes shown on a screen. Monkeys drew the shapes as sequences of mo...

متن کامل

Depth Image Sequences Compression Using Programmable Graphics Hardware

Depth image-based representation (DIMR) has recently been adopted into MPEG-4 Animation Framework eXtension (AFX). However, how to compress depth image sequences is not addressed. In this paper, we discuss the compression of the depth image sequences. Since depth maps should be kept accurate for many application scenarios, they are compressed losslessly using run-length like coding scheme. One ...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2000